var endDate=echartUtil.GetDateStr(0);
var startDate=echartUtil.GetDateStr(-10);;
var table;
var summaryTable;
var printTable;
//相关业务逻辑
$(function(){
	page.bindTag();
	page.getAllSearchWhere();
	page.getCustomerName();
	$("#startDay").val(startDate);
	$("#yestarday").val(endDate);
});


function dataChange(){
	startDate=$("#startDay").val();
	endDate=$("#yestarday").val();
	initTime=$("#initTime").val();
	endTime=$("#endTime").val();
}

 
var page={//页面总变量
		bindTag:function(){
			$("#queryWarehouseBtn").bind("click",function(){
				var object = page.getSendParam();
				if(object.warehouse===null || object.warehouse===""){
					 layer.msg("请选择仓库");
					 return false;
				}
				//查询出入库数据
				page.getWarehouseInOutDetail(object); 
				//查询出入库金额汇总
				page.getWarehouseInOutSumMoney(object);
			});
			$("#exportExcel").bind("click",function(){
				var object = page.getSendParam();
				var sendParam = "";
				for(var i in object){
					if(object[i]!==null){
						sendParam+=i+"="+object[i]+"&"
					}
				}  
				sendParam = sendParam.substring(0,sendParam.length-1);
				var url = "companyMaterial/exportExcelFileByWarehouseSumMoney?"+sendParam;
				tableUtil.download(url);
			})
			
			$("#warehouseTable tbody").on("click","input",function(event){
				event.stopPropagation(); 
			})
			
			//点击打印时调用
			$("#printWarehouse").on("click",function(){
				page.printWarehouse();
			})
		},  
		pageSearchWhereArray:[],//物料筛选条件数组（物料信息）
		pageIdArray:['providerName','materialName','model','color'],
		pageFinishWhereArray:[],//成品筛选条件数组（订单信息）
		pageFinishIdArray:['factoryId','coustomId','orderId'],
		getAllSearchWhere:function(){/**获取所有筛选条件*/
			ajaxUtil.ajaxRuquestNoQuan('companyMaterial/getAllSearchWhere',null,function(data){
				page.pageSearchWhereArray=data.materialSet;
				page.pageFinishWhereArray=data.orderSet;
				filterInput.filterView("",page.pageIdArray,page.pageSearchWhereArray);
				filterInput.filterView("",page.pageFinishIdArray,page.pageFinishWhereArray);
			}); 
		},
		getSendParam:function(){
			/**
			 * 配置要素绑定
			 */
			for(var i=0;i<page.pageIdArray.length;i++){
				$("#"+page.pageIdArray[i]).bind("change",function(){
					filterInput.filterView("",page.pageIdArray,page.pageSearchWhereArray);
				});
			}
			for(var i=0;i<page.pageFinishIdArray.length;i++){
				$("#"+page.pageFinishIdArray[i]).bind("change",function(){
					filterInput.filterView("",page.pageFinishIdArray,page.pageFinishWhereArray);
				});
			}
			var object = new Object();
			var modelVal = model=$("#model").val().trim();
			if(modelVal!==""){
				object.model=modelVal;
			} 
			object.providerName=$("#providerName").val().trim();
			var colorVal = $("#color").val().trim();
			if(colorVal!==""){
				object.color = colorVal;
			} 
			object.materialName=$("#materialName").val().trim();
			object.customerName = $("#customerName").val().trim();
			object.type=$("#type").val();
			object.queryType=$("#queryType").val();
			object.orderId = $("#orderId").val();
			object.factoryNo = $("#factoryId").val();
			object.customerNo = $("#coustomId").val();
			object.startDate = $("#startDay").val();
			object.endDate = $("#yestarday").val();
			object.status = 1;
			object.warehouse = $("#warehouse").val();
			return object;
		},
		checkboxPrint:function(el){
			var $tr = $(el).parents("tr"); 
			var customerNameLine = $tr.find("td").eq(15).text().trim();
			var customerNameVal = $("#customerName").val().trim();
			if(customerNameVal===null || customerNameVal === ''){
				 $(el).prop("checked",false);
				 layer.msg("请先筛选客户,如果无客户可选,说明暂无出库给可的订单!");
				 return; 
			} 
			if(customerNameVal !== customerNameLine){
				$(el).prop("checked",false);
				 layer.msg("筛选的客户与需打印的客户名不一致!");
			} 
		},
		//获取出入库金额汇总
		getWarehouseInOutSumMoney(object){
			ajaxUtil.ajaxRuquest("warehouseInSumMoney/getWarehouseInOutMoneyCount",object,function(data){
				$("#sumMoney .warehouseOutSumMoney").html(data.warehouseOutSumMoney);
				$("#sumMoney .warehouseInSumMoney").html(data.warehouseInSumMoney);
			});
		},
		getWarehouseInOutDetail:function(object){
			$("#allOrder").show();
			$("#printDiv").hide();
			table=ajaxUtil.dataTablesAjaxNotFxiaction("warehouseTable",8,"warehouseInSumMoney/getWarehouseInOutDetailSumMoney",
					object,[ 
					{"data":"type",render:function(data,type,row){ 
						return "<input type = 'checkbox' id='"+row.id+"' onclick='page.checkboxPrint(this)' />";
					}}, 
					{"data":"type",render:function(data,type,row){
						if(data==2 || data==5|| data==9){
							return "出库"; 
						}
						if(data==1 || data==3 || data==4 || data==7 || data==8){
							return "入库";
						}
					}}, 
					{"data":"status",render:function(data,type,row){
						return "审核通过";
					}},
					{"data":"count"},  
					{"data":"inCountPrice"}, 
					{"data":"inOutUnit"}, 
					{"data":"materialName"},
					{"data":"model"}, 
					{"data":"color"},
					{"data":"providerName"},
					{"data":"colorDescription"},
					{"data":"companyOrderId"},
					{"data":"factoryNo"},
					{"data":"customerNo"},
					{"data":"remark"},
					{"data":"customerName",render:function(data,type,row){
						return (data===null?"":data);
					}},  
					{"data":"createUser"},
					{"data":"createTime"},
					{"data":"approvalUser"},
					{"data":"approvalTime"},
					{"data":"position"}
					
			],[0],function(td, cellData, rowData, row, col){
						if(rowData.type==2 || rowData.type==5){
							$(td).parent().addClass("twoTwo"); 
						}
				    });
		},
		//获取出入库中的客户名称
		getCustomerName:function(){
			ajaxUtil.ajaxRuquest("basicData/getCustomerNameByWarehouse",{},function(data){
				var customerNameList = $("#customerNameList");
				if(data!=null && data.length>0){					
					var option = "";
					for(var i = 0;i<data.length;i++){
						option+=" <option value='"+data[i]+"'>"+data[i]+"</option>   ";
					}
					customerNameList.append(option);
				}
			});
		},
		//获取出入库需打印信息
		getWarehouseInOutDetailPrint:function(object){
			ajaxUtil.ajaxRuquest("companyMaterial/getWarehouseInOutDetailByPrint",object,function(data){
				var html = ""; 
				if(data !=null && data.length>0){
					for(var i=0;i<data.length;i++){  
						html+="<tr>" + 
								"<td>"+(data[i].materialName==null?"":data[i].materialName)+"</td>"+
								"<td>"+(data[i].model==null?"":data[i].model)+"</td>"+
								"<td>"+(data[i].color==null?"":data[i].color)+"</td>"+
								"<td>"+(data[i].colorDescription==null?"":data[i].colorDescription)+"</td>"+
								"<td>"+(data[i].factoryNo==null?"":data[i].factoryNo)+"</td>"+
								"<td>"+(data[i].customerNo==null?"":data[i].customerNo)+"</td>"+
								"<td>"+(data[i].count==null?0:data[i].count)+"</td>"+
								"<td>"+(data[i].inOutUnit==null?"无":data[i].inOutUnit)+"</td>"+
								"<td>"+(data[i].inCountPrice==null?0:data[i].inCountPrice).toFixed(2)+"</td>"+
								"<td>"+(data[i].count*data[i].inCountPrice).toFixed(2)+"</td>"+
								"<td>"+(data[i].remark==null?"":data[i].remark)+"</td>"+ 
							  "</tr>"; 
					}
					//发货人
					var loginUser = window.sessionStorage.getItem("user");
					if(loginUser!==undefined){
						var user = JSON.parse(loginUser);
						$("#consigner").html(user.userName); 
					}
					//收货客户
					var receiveCustomer = $("#customerName").val();
					$("#receiveCustomer").html(receiveCustomer);
					//送货时间 
					var deliveryTime = moment().format("YYYY-MM-DD HH:mm:ss");
					$("#createTime").html(deliveryTime);  
					$("#warehouseTablePrint tbody").html(html); 
					//调用客户信息.进行打印
					window.print(); 
				} 
			});
		},
		//打印记录
		printWarehouse:function(){
			//需要打印的物料数组
			var warehouseArray = [];
			$("#warehouseTable tbody tr").each(function(){
				var checked = $(this).find("td").eq(0).find("input").is(":checked");
				if(checked){ 
					var id = $(this).find("input").attr("id")	
					warehouseArray.push(id);
				}
			}); 
			var ids = warehouseArray.join(",");
			var object = {
					"ids":ids
			}; 
			if(object.ids===null || object.ids===""){
				layAlert("未勾选需要打印的出入库记录");
				return false;
			}
			page.getWarehouseInOutDetailPrint(object);
		}
}
